package cn.openea.perpartner.meily.web.controller;


import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.text.Convert;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.Enterprise;
import cn.openea.perpartner.meily.web.entity.Goods;
import cn.openea.perpartner.meily.web.entity.dto.EnterpriseDtoFactory;
import cn.openea.perpartner.meily.web.entity.vo.EnterpriseRelevancyItemVo;
import cn.openea.perpartner.meily.web.entity.vo.EnterpriseSearchVo;
import cn.openea.perpartner.meily.web.service.IEnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * (Enterprise)表控制层
 *
 * @author Weird077
 * @since 2023-05-28 10:23:37
 */
@Api(tags = "企业管理")
@RestController
@RequestMapping("enterprise")
public class EnterpriseController extends BaseController {

    @Resource
    private EnterpriseDtoFactory enterpriseDtoFactory;
    @Resource
    private IEnterpriseService enterpriseService;

    @ApiOperation(value = "企业列表", notes = "企业列表")
    @PostMapping("/page/list")
    public TableDataInfo selectEnterpriseList(@RequestBody EnterpriseSearchVo searchVo) {
        startPage();
        List<Enterprise> list = enterpriseService.selectList(searchVo);
        return getDataTable(list);
    }

    @ApiOperation(value = "企业列表 - 小程序专用", notes = "企业列表")
    @PostMapping("/applet/page/list")
    public TableDataInfo selectAppPageList(@RequestBody EnterpriseSearchVo searchVo) {
        startPage();
        List<Enterprise> list = enterpriseService.selectList(searchVo);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setRows(list.parallelStream()
                .map(item -> enterpriseDtoFactory.toIntegratedDetailsDto.apply(item))
                .collect(Collectors.toList()));
        return dataTable;
    }

    @ApiOperation(value = "企业关联项目数量")
    @GetMapping("/relevancy/items/{userId}/info")
    public AjaxResult relevancyItemsInfo(@PathVariable String userId) {
        Enterprise enterprise = Optional.ofNullable(enterpriseService.getById(userId))
                .orElseThrow(() -> new ServiceException("企业不存在"));
        return AjaxResult.success(enterpriseDtoFactory.toRelevancyItemQuantityDto.apply(enterprise));
    }

    @ApiOperation(value = "企业关联项目")
    @PostMapping("/relevancy/items")
    public AjaxResult relevancyItems(@RequestBody @Valid EnterpriseRelevancyItemVo relevancyItemVo) {
        enterpriseService.relevancyItems(relevancyItemVo);
        return AjaxResult.success();
    }

    @ApiOperation(value = "所有企业列表")
    @GetMapping("/all/list")
    public AjaxResult allList() {
        List<Enterprise> list = enterpriseService.selectAll();
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "企业详情-包含所关联的企业")
    @GetMapping("/details/info//{userId}")
    public AjaxResult selectDetailsInfo(@PathVariable Long userId) {
        Enterprise enterprise = Optional.ofNullable(enterpriseService.getById(userId))
                .orElseThrow(() -> new ServiceException("企业不存在"));
        return success(enterpriseDtoFactory.toIntegratedDetailsDto.apply(enterprise));
    }

    @ApiOperation(value = "企业详情-基本信息")
    @GetMapping("/basic/info/{userId}")
    public AjaxResult selectBasicInfo(@PathVariable Long userId) {
        Enterprise enterprise = Optional.ofNullable(enterpriseService.getById(userId))
                .orElseThrow(() -> new ServiceException("企业不存在"));
        return success(enterprise);
    }

    @ApiOperation(value = "新增企业", notes = "新增企业")
    @PostMapping("/add")
    public AjaxResult addEnterprise(@RequestBody Enterprise enterprise) {
        enterpriseService.addEnterprise(enterprise);
        return AjaxResult.success();
    }

    @ApiOperation(value = "修改企业", notes = "修改企业")
    @PostMapping("/edit")
    public AjaxResult updateEnterprise(@RequestBody Enterprise enterprise) {
        enterpriseService.edit(enterprise);
        return success();
    }

    @ApiOperation(value = "授权转移", notes = "授权转移")
    @GetMapping("/transfer/{userId}")
    public AjaxResult transferEnterprise(@PathVariable String userId) {
        Enterprise enterprise = Enterprise.builder()
                .userId(Convert.toLong(userId))
                .transferStatus("1")
                .build();
        return success(enterpriseService.updateById(enterprise));
    }

    @ApiOperation(value = "删除企业", notes = "删除企业")
    @PostMapping("/delete")
    public AjaxResult deleteEnterprise(@RequestBody List<String> idList) {
        boolean result = enterpriseService.deleteEnterprise(idList);
        return toAjax(result);
    }

    @ApiOperation(value = "生成授权码", notes = "生成授权码")
    @PostMapping("/qr/code")
    public AjaxResult createQrCode(@RequestBody Enterprise enterprise) {
        String cosUrl = enterpriseService.createQrCode(enterprise.getUserId());
        return AjaxResult.success().put("cosUrl", cosUrl);
    }

    @ApiOperation("推荐企业")
    @GetMapping("/top/{userId}")
    public AjaxResult top(@PathVariable Long userId) {
        return toAjax(this.enterpriseService.top(userId));
    }

    @ApiOperation("取消推荐")
    @GetMapping("/top/cancel/{userId}")
    public AjaxResult topCancel(@PathVariable Long userId) {
        return toAjax(this.enterpriseService.topCancel(userId));
    }

    @ApiOperation(value = "推荐置顶企业列表")
    @PostMapping("/page/top/list")
    public TableDataInfo getTopList() {
        startPage();
        List<Enterprise> list = enterpriseService.getTopList();
        return getDataTable(list);
    }

    @ApiOperation(value = "随机企业用户资源")
    @PostMapping("/page/random/list")
    public AjaxResult getRandomList(@RequestBody EnterpriseSearchVo vo) {
        return AjaxResult.success("获取企业资源成功！", enterpriseService.getRandomList(vo));
    }

    @ApiOperation(value = "获取相似企业列表")
    @PostMapping("/page/similar/list")
    public AjaxResult getSimilarList(@RequestBody EnterpriseSearchVo vo) {
        List<EnterpriseDtoFactory.IntegratedDetailsDto> list = enterpriseService.getSimilarList(vo).parallelStream()
            .map(item -> enterpriseDtoFactory.toIntegratedDetailsDto.apply(item))
            .collect(Collectors.toList());
        return AjaxResult.success("获取相似企业列表成功！", list);
    }

    @ApiOperation(value = "获取企业关联品牌")
    @PostMapping("/page/associate/brand/list")
    public TableDataInfo getAssociateBrandList(@RequestBody EnterpriseSearchVo vo) {
        startPage();
        List<Goods> list = enterpriseService.getAssociateBrandList(vo);
        return getDataTable(list);
    }

}

